package com.caidanxiaoyuan.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.caidanxiaoyuan.model.ShopList;
import com.caidanxiaoyuan.model.ShopListView;
import com.caidanxiaoyuan.model.ShopLiuYanList;
import com.caidanxiaoyuan.model.ShopLiuyanListView;
import com.caidanxiaoyuan.service.ShopListViewService;
import com.caidanxiaoyuan.service.ShopLiuYanListService;
import com.caidanxiaoyuan.service.ShopLiuyanListViewService;
import com.caidanxiaoyuan.util.JsonUtil;
import com.caidanxiaoyuan.util.ResponseUtil;
import com.github.pagehelper.Page;

import ShopListMapper.ShopListService;

@RequestMapping("/shop")
@CrossOrigin
@Controller
public class ShopController {
	@Autowired
	private ShopListService shopListService;
	@Autowired
	private ShopListViewService shopListViewService;
	@Autowired
	private ShopLiuyanListViewService shopLiuyanListViewService;
	@Autowired
	private ShopLiuYanListService shopLiuYanListService;
	/**
	 * @throws Exception 
	 * 
	* @Description: 删除产品  
	* @author zealot
	* @param @param e 
	* @return void    
	* @version 创建时间：2017年2月28日 下午3:15:51  
	* @throws
	 */
	@RequestMapping("/delete")
	public void delete(ShopList e,HttpServletResponse response) throws Exception{
		shopListService.deleteByPrimaryKey(e.getId().toString());
		ResponseUtil.write(response, 12);
	}
	
	/**
	 * 
	* @Description: 个人商店  
	* @author zealot
	* @param  
	* @return void    
	* @version 创建时间：2017年2月28日 下午3:29:45  
	* @throws
	 */
	@RequestMapping("/view")
	public void view(ShopList e,HttpServletResponse response){
		ResponseUtil.result(response, JsonUtil.toJsonStr(shopListService.selectByPrimaryKey(e.getId())));
	}
	
	/**
	 * 
	* @Description: 产品编辑  
	* @author zealot
	* @param @param e
	* @param @param response
	* @param @throws Exception 
	* @return void    
	* @version 创建时间：2017年2月28日 下午4:29:23  
	* @throws
	 */
	@RequestMapping("/edit")
	public void edit(ShopList e,HttpServletResponse response) throws Exception{
		 switch(e.getAction())
		    {
		        case "append":
		        	e.setShijian((int) Long.parseLong(String.valueOf(System.currentTimeMillis()).toString().substring(0,10)));
		        	shopListService.insert(e);
		        	Map<String, Object> map = new HashMap<String, Object>();
		        	map.put("id", e.getId());
		        	ResponseUtil.result(response, JsonUtil.toJsonStr(map));
		            break;
		        case "update":
		            shopListService.update(e);
		            break;
		    }
		 ResponseUtil.write(response, 12);
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> shopListView(Page<ShopListView> page,ShopListView t){
		
		Map<String, Object> map = new HashMap<String, Object>();
		Page<ShopListView> list = (Page<ShopListView>) shopListViewService.selectAll(page,t);
		map.put("data",list.getResult());
		map.put("totalpage", list.getPages());
		map.put("totalrows", list.getTotal());
		map.put("time", String.valueOf(System.currentTimeMillis()).toString().substring(0,10));
		return map;
	}
	
	/**
	 * 
	* @Description: 市场H5页面  
	* @author zealot
	* @param @param t
	* @param @param request
	* @param @return 
	* @return String    
	* @version 创建时间：2017年3月1日 上午11:18:59  
	* @throws
	 */
	@RequestMapping("/h5/shichang")
	public String shenghuo(ShopList t,HttpServletRequest request){
		t = shopListService.selectByPrimaryKey(t.getId());
		request.setAttribute("e", t);
		ShopLiuyanListView ly =  shopLiuyanListViewService.selectByPrimaryKey(t.getId());
		request.setAttribute("l", ly);
		return "h5/shichang";
	}
	
	/**
	 * 
	* @Description: 留言列表  
	* @author zealot
	* @param @param page
	* @param @param e
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年3月1日 上午11:21:53  
	* @throws
	 */
	@RequestMapping("/liuyan/list")
	public void liuyanList(Page<ShopLiuyanListView> page,ShopLiuyanListView  e,HttpServletResponse response){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", shopLiuyanListViewService.selectAll(page, e));
		ResponseUtil.result(response,JsonUtil.toJsonStr(map));
	}
	
	/**
	 * 
	* @Description: 内容提交  
	* @author zealot
	* @param @param page
	* @param @param e
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年3月1日 上午11:31:19  
	* @throws
	 */
	@RequestMapping("/liuyan/edit")
	public void liuyanEdit(ShopLiuYanList  e,HttpServletResponse response){
		e.setShijian((int) Long.parseLong(String.valueOf(System.currentTimeMillis()).toString().substring(0,10)));
		int id = shopLiuYanListService.insert(e);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		ResponseUtil.result(response,JsonUtil.toJsonStr(map));
	}
}
